package com.sc.web.gwt.client.presenter;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.sc.web.gwt.client.ClientFactory;
import com.sc.web.gwt.client.event.TextBoxValidation;
import com.sc.web.gwt.client.event.TextBoxValidationHandler;
import com.sc.web.gwt.client.place.RegisterWidgetPlace;
import com.sc.web.gwt.client.view.registerwidget.RegisterWidgetView;

public class RegisterActivity extends AbstractActivity implements RegisterWidgetView.Presenter {
    // Used to obtain views, eventBus, placeController
    // Alternatively, could be injected via GIN
    private ClientFactory clientFactory;
    private EventBus eventBus;
    private RegisterWidgetView registerWidgetView;

    public RegisterActivity(RegisterWidgetPlace place, ClientFactory clientFactory) {
        this.clientFactory = clientFactory;
        eventBus = clientFactory.getEventBus();
    }

    public void start(AcceptsOneWidget panel, EventBus eventBus) {
        registerWidgetView = clientFactory.getRegisterWidgetView();
        registerWidgetView.setPresenter(this);
        panel.setWidget(registerWidgetView.asWidget());
        eventBus.addHandler(TextBoxValidation.TYPE, new TextBoxValidationHandler() {
            public void onTextBoxValidation(TextBoxValidation event) {
                // show error or ok result next to validated text-box.
            }
        });
    }

    public void goTo(Place place) {
        clientFactory.getPlaceController().goTo(place);
    }

    public void clearFields() {
        registerWidgetView.clearFields();
    }

    public void validateTextBox() {
        eventBus.fireEvent(new TextBoxValidation());
    }
}
